<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\View;


class Relation extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\User';

    public function index(): ?string
    {
        $algebra = $this->request->param('algebra');
        if ($algebra && $algebra > 0) {
            return $this->algebra($this->request->param());
        }
        $keyword = $this->request->param('keyword');
        if ($keyword) {
            $map['code|nickname|mobile'] = $keyword;
        } else {
            $map['code'] = 'admin';
        }
        $first = $this->model::where($map)->field('id,code,nickname,mobile')->find();
        if ($first) {
            $level = $first->level ? $first->level->title : '暂无';
            $first['title'] = '编号【' . $first->code . '】
                姓名【' . $first->nickname . '】
                手机号【' . $first->mobile . '】
                等级【' . $level . '】
               ';
        }

        View::assign('first', $first);
        return View::fetch();

    }

    public function algebra($param)
    {
        $code = $param['keyword'] ?: 'admin';

        $list = \app\common\model\Relation::where('code', $code)->with('user')
            ->where('algebra', '<=', $param['algebra'])->order('algebra', 'asc')
            ->field('code,new_uid,new_code,algebra,first_uid')
            ->select();

        $algebra = 1;
        foreach ($list as $k => $v) {
            if (isset($in[$v->algebra-1][$v->first_uid])) {
                $b = [
                    'title' => $v->new_code,
                    'id' => $v->new_uid
                ];
                if (isset($in[$v->algebra-1][$v->first_uid]['children'])) {

                    $in[$v->algebra-1][$v->first_uid]['children'] = array_merge($in[$v->algebra-1][$v->first_uid]['children'], $b);
                }else{
                    $in[$v->algebra-1][$v->first_uid]['children'] = $b;
                }
            } else {
                $in[$v->algebra-1][$v->first_uid] = [
                    'title' => $v->new_code,
                    'id' => $v->new_uid
                ];
            }

        }

        dump($in);die;


        return View::fetch('algebra');
    }

    public function getData()
    {
        try {

            $id = $this->request->param('id');
            $list = $this->model::where('parent_id', $id)->field('id,code,nickname,mobile')->select();
            foreach ($list as &$v) {
                $level = $v->level ? $v->level->title : '暂无';
                $v['title'] = '编号【' . $v['code'] . '】
                    姓名【' . $v->nickname . '】
                    手机号【' . $v->mobile . '】
                    等级【' . $level . '】
                  ';
            }
            return json(['code' => 1, 'msg' => 'success', 'result' => $list]);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

    }


}
